package cn.biplam.setup.code;

import java.util.ArrayList;

import cn.biplam.common.client.DataFilter;
import cn.biplam.common.paramters.SysCaption;
import cn.biplam.common.paramters.SysName;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.common.wrapper.sqlresult.SqlResult;
import cn.biplam.front.component.AjxResponse;
import cn.biplam.front.component.data.common.ButtonComp;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.HtmlValue;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Clear_Type;
import cn.biplam.front.html.base.Const.Float_Type;
import cn.biplam.front.html.base.Const.Window_Event;
import cn.biplam.front.htmlscript.script.AjxScript;
import cn.biplam.front.htmlscript.script.PopupScript;
import cn.biplam.front.htmlscript.script.AjxScript.Is_SubMitData;
import cn.biplam.front.template.base.Anchor;
import cn.biplam.front.template.control.Confirm;
import cn.biplam.front.template.control.table.TablePropertys;

/**
 * 交换代码管理
 * @author XiaoRuxing Sep 22, 20097:02:36 PM
 * @version 1.0
 */
public abstract class CodeManage extends AjxResponse{

	protected String cId;
	/** 进入交换代码管理界面 */
	protected String aId_main ;
	/** 添加代码界面 */
	protected String Add_Panel ;
	/** 添加代码操作 */
	protected String Add_Panel_Opt ;
	/** 修改代码界面 */
	protected String Edit_Panel ;
	/** 修改代码操作 */
	protected String Edit_Panel_Opt ;
	/** 删除代码界面 */
	protected String Del_Panel ;
	/** 删除代码操作 */
	protected String Del_Opt ;
	/** 进入下级代码管理 */
	protected String View_Next_Level ;
	/** 查找 */
	protected String Search ;
	
	protected String codeType;
	protected String codeId;
	protected ExchangeCodeData dataOpt;
	protected CodeManageBean bean;
	
	protected String codeContainerId = cId+"ccd";
	protected String codeEditTableId = cId+"etd";
	
	
	@Override
	protected HtmlWindow ajxGet() throws ErrorMessage {
		if(actionId.equals(aId_main)){
			this.codeId = null;
			return get();
		}else if(actionId.equals(View_Next_Level)){
			setCodeId();
			return get();
		}else if(actionId.equals(Add_Panel)){
			return _getAddPanel();
		}else if(actionId.equals(Add_Panel_Opt)){
			return _getAddOpt();
		}else if(actionId.equals(Edit_Panel)){
			return _getEditPanel();
		}else if(actionId.equals(Edit_Panel_Opt)){
			return _getEditOpt();
		}else if(actionId.equals(Del_Panel)){
			return _getDelPanel();
		}else if(actionId.equals(Del_Opt)){
			return _getDelOpt();
		}else if(actionId.equals(Search)){
			return searchOpt();
		}else{
			throw new ErrorMessage("actionid is not found:"+actionId+"!");
		}
	}
	
	public CodeManage(String cId){
		this.cId = cId;
		 aId_main = cId+"01";
		 Add_Panel = cId+"03";
		 Add_Panel_Opt = cId+"04";
		 Edit_Panel = cId+"05";
		 Edit_Panel_Opt = cId+"06";
		 Del_Panel = cId+"07";
		 Del_Opt = cId+"08";
		 View_Next_Level = cId+"09";
		 Search = cId+"10";
		 codeContainerId = cId+"ccd";
		 codeEditTableId = cId+"etd";
	}
	
	/**
	 * 设置代码ID
	 */
	protected abstract void setCodeId();
	
	/**
	 * 获取代码管理界面
	 * @return
	 * @throws ErrorMessage
	 */
	private HtmlWindow get() throws ErrorMessage{
		HtmlDiv  div = new HtmlDiv(cId);
		//放入当前记录ID
		div.put(new HtmlValue(cId+"r",cId+"r",recordId));
		//生成查找输入框
		div.put(searchExchangeClass());
		//获取标题栏目
		HtmlWindow title = getTitleMenu();
		title.setClear(Clear_Type.Both);
		div.put(title);
		
		//生成代码内容栏
		SqlResult codes = dataOpt.getUnderCodes(codeId);
		HtmlWindow content = getCodeContent(codes);
		content.setClear(Clear_Type.Both);
		content.setFloat(Float_Type.Left);
		div.put(content);
		
		return div;
	}
	
	/**
	 * 获取标题(当前代码的导航路径)
	 * @return
	 * @throws ErrorMessage
	 */
	private HtmlWindow getTitleMenu() throws ErrorMessage{
		ExchangeCodeData codeData = dataOpt;
		ArrayList<String[]> parents =  codeData.getParents(codeId);
		HtmlWindow div = new HtmlDiv();
		Anchor anchor = new Anchor(AjxScript.getA(cId, aId_main, codeType),
				"所有"+">");
		div.put(anchor.get());
		for(String[] pkey:parents){
			String pId = pkey[0];
			String pName = pkey[1];
			anchor = new Anchor(AjxScript.getA(cId, View_Next_Level, pId),
					pName+">");
			div.put(anchor.get());
		}
		HtmlWindow button = ButtonComp.get(mUser, SysCaption.Create);
		button.bind(Window_Event.OnClick,AjxScript.getPr(cId, Add_Panel, Is_SubMitData.Submit));
		button.setFloat(Float_Type.Right);
		div.put(button);
		div.setStyleHeight("22");
		return div;
	}
	
	/**
	 * 查找交换类
	 * @return
	 * @throws ErrorMessage 
	 */
	private HtmlWindow searchExchangeClass() throws ErrorMessage{
		String script = AjxScript.getSp(cId, Search, cId);
		return new CodeManageBean().searchExchangeClass
				(dataOpt, script);
	}
	
	/**
	 * 查找操作
	 * @return
	 * @throws ErrorMessage
	 */
	private HtmlWindow searchOpt() throws ErrorMessage{
		HtmlDiv  div = new HtmlDiv(cId);
		div.put(new HtmlValue(cId+"r",cId+"r",recordId));
		div.put(searchExchangeClass());
		DataFilter filter = new DataFilter();
		filter.putAll(params);
		SqlResult codes = dataOpt.search(filter);
		HtmlWindow content = getCodeContent(codes);
		content.setClear(Clear_Type.Both);
		content.setFloat(Float_Type.Left);
		div.put(content);
		return div;
	}
	
	/**
	 * 获取添加界面
	 * @return
	 * @throws ErrorMessage
	 */
	private HtmlWindow _getAddPanel() throws ErrorMessage{
		String codeId = dataOpt.getCodeId(recordId);
		HtmlWindow win = _getPanel(mUser, null, false,codeId);
		String script = AjxScript.getSpNoRefresh(
				codeEditTableId, //提交容器ID
				Add_Panel_Opt    //提交动作ID
				);
		win.bind(SysName.PromptSubmitID,Window_Event.OnClick,PopupScript.prSubmit(script));//关闭Pr窗口
		return win;
	}
	
	/**
	 * 添加代码操作
	 * @return
	 * @throws ErrorMessage 
	 * @throws ErrorMessage
	 */
	protected abstract HtmlWindow _getAddOpt() throws ErrorMessage;
	
	/**
	 * 修改代码操作
	 * @return
	 * @throws ErrorMessage
	 */
	private HtmlWindow _getEditOpt() throws ErrorMessage{
		//更新当前修改
		DataFilter filter = new DataFilter();
		filter.putAll(params);
		filter.put(dataOpt.CODE_ID, recordId);
		filter.put(dataOpt.PRODUCT_ID, params.getByKey(dataOpt.PRODUCT_ID+"value"));
		dataOpt.UpdateCode(filter);
		
		//刷新脚本
		HtmlWindow win = new HtmlDiv();
		String refreshId = dataOpt.getParent(recordId,codeType);
		win.bind(AjxScript.getA(cId, View_Next_Level,refreshId));
		return win;
	}
	
	/**
	 * 获取修改界面
	 * @return
	 * @throws ErrorMessage
	 */
	private HtmlWindow _getEditPanel() throws ErrorMessage{
		SqlResult sr = dataOpt.getCode(recordId);
		HtmlWindow win = _getPanel(mUser, sr, true, null);
		String script = AjxScript.getSpNoRefresh(
				codeEditTableId, //提交容器Id
				Edit_Panel_Opt   //提交动作ID
				);
		win.bind(SysName.PromptSubmitID, Window_Event.OnClick, PopupScript.prSubmit(script));//关闭提交窗口
		return win;
	}
	
	/**
	 * 删除代码
	 * @return
	 * @throws ErrorMessage
	 */
	private HtmlWindow _getDelPanel() throws ErrorMessage{
		String msg = "确定删除代码?";
		Confirm delete =new Confirm(msg,recordId);
		delete.bind(Del_Opt);
		return delete.get();
	}
	
	/**
	 * 删除代码操作
	 * @return
	 * @throws ErrorMessage 
	 */
	private HtmlWindow _getDelOpt() throws ErrorMessage{
		String refreshId = dataOpt.getParent(recordId,codeType);
		//删除操作
		dataOpt.delSubCodes(recordId);
		//刷新脚本
		HtmlWindow win = new HtmlDiv();
		win.bind(AjxScript.getA(cId, View_Next_Level,refreshId));
		return win;
	}
	
	/**
	 * 获取编辑界面
	 * @param mUser
	 * @param sr
	 * @return
	 * @throws ErrorMessage
	 */
	private HtmlWindow _getPanel(UserInfo mUser,SqlResult sr,boolean edit,String codeId) throws ErrorMessage{
		return bean.getPanel(mUser, sr, edit, codeId, dataOpt, codeEditTableId, recordId, sHomeUrl);
	}
	
	/**
	 * 获取代码内容
	 * @return
	 * @throws ErrorMessage
	 */
	private HtmlWindow getCodeContent(SqlResult codes) throws ErrorMessage{
		String tablePageID = codeContainerId;
		TablePropertys property = new CodeManageBean().getProperty(sHomeUrl, mUser, 
				tablePageID, dataOpt, View_Next_Level, Edit_Panel, Del_Panel, cId);
		return bean.getCodeContent(tablePageID, codes, dataOpt, property);
	}
}
